 
/**
 * redmicro all Copyright (c)
 * Created by seeker910 on 2015/6/30.
 * 数据源来自DataStore,在DataStore数据量不大时，或数据已加载到本地时，适合使用该控件。
 */
Rsd.define('Rsd.form.ComboBoxEx', {
    extend: 'Rsd.form.ComboBox',
    xtype: 'combobox-ex',
    /**
     * @description Rsd.data.Store数据源
     * {
     * xtype:'liststore',
     * listMember:'rows'
     * }
     */
    dataStore:null,
    /*
    * */
    constructor: function ComboBoxEx (config) {
        this.apply(config||{});
    },
    /** 
    * @private
    * 未实现dataStore带筛选的功能数据加载
    * */
    onAfterInit:function onAfterInit()
    {
        var me = this;
        if(Rsd.isString(this.dataStore))
        {
            this.dataStore = Rsd.widget(this.dataStore,{});
        }
        if(Rsd.isObject(this.dataStore) && Rsd.isEmpty(this.dataStore.$className))
        {
            this.dataStore = Rsd.widget(this.dataStore.xtype||'liststore',this.dataStore);
        }
        try
        {
            this.dataStore.load(this.args||{},function(data){

                var _list = null;
    
                if(Rsd.isEmpty(me.dataStore.listName) )
                {
                    _list = data.hasOwnProperty('data')?data.data:data;
                }
                else
                {
                    if(data.data && !data.data.hasOwnProperty(me.dataStore.listName))
                    {
                        me.error('Data has no property(listName is ['+me.dataStore.listName+']).',data.data);
                    }
                    _list = data.data[me.dataStore.listName];
                }
    
                me.dataSource =_list;
                //设置value
                me.callParentFn('onAfterInit');
            });
        }
        catch(ex)
        {
            console.error("加载数据源失败：" + ex.toString());
        } 
      
    },
    /** 
    * @private 
    * 在表格列属性中需要设置以下属性
     * config:{
     *     dataStore ：数据源，
     *     textMember ：现实到文本列名称，
     *    「colorMember」：颜色属性，选填
     * } 
     * 
     * @param {*} config  
     * @param {*} row 
     * @param {*} state 是否是编辑状态
     * @returns 返回 对象或数组
     */
    makeControl:function makeControl(config,row,state)
    {
        if(Rsd.isEmpty(row))
        {
            return[];
        }
        var _config = config ||{};
        var _editable = _config.editable;

        var _value = row[_config.dataIndex|| _config.dataindex || _config.name];
        if(Rsd.isEmpty(_value))
        {
            return [];
        }

        var _textMember = _config.textMember || 'text';
        var _colorMember = _config.colorMember || 'color';
 
        if(Rsd.isString( _config.dataStore))
        {
            _config.dataStore = Rsd.widget(_config.dataStore,{});
        }
        
        var _ctrl = null;

      
        if(_editable && state)
            {
                var _ctrl = null; 
                var editor = _config.editor||{label:false,width:'95%'};
                editor.dataSource = _config.dataSource;
                editor.textMember = _config.textMember||'text',
                editor.valueMember = _config.valueMember||'code',
                editor.colorMember= _config.colorMember||'color', 
                _ctrl = Rsd.create('Rsd.form.ComboBoxEx',editor);//console.log(_value,editor);
                _ctrl.setValue(_value);
                return [_ctrl]
            }
        else
        {
            _ctrl = document.createElement('label');
            _ctrl.style.position = 'relative';
            _ctrl.style.width = '100%';
            _ctrl.style.height = '100%';
            if(Rsd.isEmpty(_config.dataStore))
            {
                 console.error("dataStore is nulll."); 
            }
            else
            {
                _config.dataStore.load(_config.args||{},function(data){
 
                    var _txt = _value;
                    _config.dataStore.getObject(_value,function (obj) {
                        var _item = obj;
                        var _timer = 0;
                        if(_item)
                        {
                            _timer = 10;
                            _txt = _item[_textMember];
                            if(_config.formatString)
                            {
                                _txt = Rsd.formatTemplateString(_config.formatString,_item);
                            }
                            if(_colorMember && _item.hasOwnProperty(_colorMember))
                            {
                                _ctrl.style.color = _item[_colorMember];
                            }
                        }
                        else
                        {
                            _timer = 50;
                        }
                        setTimeout(function () {
    
                            _ctrl.innerHTML =_txt;
    
    
                        },_timer);
                    });
    
                });
    
            }
             
        }
        return [_ctrl];
    }

});
